IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'sp_Command_Update_Status')
	DROP PROCEDURE dbo.sp_Command_Update_Status
GO
CREATE PROCEDURE dbo.sp_Command_Update_Status
	@CommandID INT
AS
BEGIN
	SET NOCOUNT OFF

	DECLARE @_CommandID INT 
	SET @_CommandID = @CommandID

	DECLARE @_TotalLine INT
	DECLARE @_TotalStatus INT

	SELECT 
		@_TotalLine = COUNT(1)
		, @_TotalStatus = SUM(CASE Status WHEN 'N' THEN 0 WHEN 'C' THEN 1 END)
	FROM 
		dbo.CommandLines 
	WHERE 
		CommandID = @_CommandID

	UPDATE dbo.Commands 
	SET 
		Status =	CASE 
						WHEN @_TotalLine < @_TotalStatus THEN 'N'
						ELSE 'C'
					END
	WHERE 
		CommandID = @_CommandID

	SELECT TOP 1 * FROM dbo.Commands WHERE 1 = 2
END
GO